package mobileserver;

import java.util.HashMap;
import java.util.Iterator;

import db.IChatFacade;
import db.JDBCChatFacade;

/**
 * the class of SessionManager stores all the IoSession
 * and its corresponding user info
 * 
 * The class is SINGLETON 
 * @author wangkaidi
 *
 */
public class SessionManager 
{
	private static SessionManager manager;
	private SessionManager()
	{
		
	}
	
	/**
	 * get the only instance
	 * @return
	 */
	public synchronized static SessionManager getInstance()
	{
		if(manager==null)
		{
			manager=new SessionManager();
		}
		return manager;
	}
	
	private HashMap<Integer,IoSession> sessionmap=new HashMap<Integer,IoSession>();//store the id to iosession hashmap
	private HashMap<Integer,String> idmap=new HashMap<Integer,String>();//store the login name to iosession hashmap
	private IChatFacade facade=JDBCChatFacade.getInstance();//get the db facade to manipulate database operation
	
	/**
	 * this method will add a new HashMap entry to the sessionmap
	 * @param id
	 * @param session
	 */
	public synchronized void addSession(int id,IoSession session)
	{
		sessionmap.put(id,session);
	}
	
	/**
	 * this method will store the login name with the session id
	 * @param id
	 * @param username
	 */
	public synchronized void setName(int id,String username)
	{
		idmap.put(id,username);
	}
	
	/**
	 * remove the IoSession from the session map 
	 * @param id
	 */
	public synchronized void removeSession(int id)
	{
		sessionmap.remove(id);
		idmap.remove(id);
	}
	
	/**
	 * this method return whether specific 
	 * IoSession existed
	 * @param session
	 * @return
	 */
	public synchronized boolean exist(IoSession session)
	{
		return sessionmap.containsValue(session);
	}
	
	/**
	 * this method will return the session id 
	 * according to the IoSession.
	 * If not existed , -1 will be returned
	 * @param session
	 * @return
	 */
	public synchronized int getIdBySession(IoSession session)
	{
		Iterator<Integer>iterator=sessionmap.keySet().iterator();
		while(iterator.hasNext())
		{
			int id=iterator.next();
			IoSession temp=sessionmap.get(id);
			if(temp.equals(session))
			{
				return id;
			}
		}
		return -1;
	}
	
	/**
	 * this method return whether specific user
	 * @param name
	 * @return
	 */
	public synchronized boolean whetherOnLine(String name)
	{
		return idmap.containsValue(name);
	}
	
	/**
	 * this method will return the session id according to the 
	 * login user name
	 * if not existed. -1 will be returned
	 * @param name
	 * @return
	 */
	public synchronized int getIdByName(String name)
	{
		Iterator<Integer>iterator=idmap.keySet().iterator();
		while(iterator.hasNext())
		{
			int id=iterator.next();
			String value=idmap.get(id);
			if(value.equals(name))
			{
				return id;
			}
		}
		return -1;
	}
	
	public synchronized String getNameById(int id)
	{
		return idmap.get(id);
	}
	
	/**
	 * this method will return the IoSession object ,
	 * if not existed,null will be returned
	 * @param name
	 * @return
	 */
	public synchronized IoSession getSessionByName(String name)
	{
		int id=this.getIdByName(name);
		if(id<0)//if not found session
		{
			return null;
		}
		else
		{
			return sessionmap.get(id);
		}
	}
	
	public synchronized int number()
	{
		return sessionmap.keySet().size();
	}
	
	public HashMap<Integer,String> getIdMap()
	{
		return idmap;
	}
}
